STK.register('app.mod.appmenu', function($){
    return function(){
        var APPMENUTEMPLATE = '<div class="container" node-type="appmenu" style="display: none;"><div class="panel">\
									<div class="panel-header"><span>应用列表</span><a class="panel-header-button">X</a><a class="panel-header-button">[]</a><a class="panel-header-button">-</a></div>\
									<div class="panel-content"><div style="height: 200px;"><div node-type="appselect" style="position: relative;float: left;"></div><ul node-type="hotapplist" class="appmenu" style="display:none;">暂无可选应用,请联系管理员</ul><ul node-type="userapplist" class="appmenu"><button node-type="addappbtn">添加热门应用</button></ul></div></div>\
								</div></div>';
		
        var LITEMPLATE = '<div><img src="http://weiyouxi-img.stor.appsina.com/029f822066c76b5e86ec6e22816418a7.png" width="60"></div>\
						  <div><p>#{name}</p><p><button action-data="aid=#{aid}" action-type="addapp">添加</button></p></div>';
		var dom = $.builder(APPMENUTEMPLATE), appmenu = dom.list['appmenu'][0], hotapplist = dom.list['hotapplist'][0],userapplist = dom.list['userapplist'][0],appselect= dom.list['appselect'][0],addappbtn= dom.list['addappbtn'][0];
        $.E('mainbox').appendChild(appmenu);
		appmenu.style.display = "";
		
		var build = function(list,type){
			var node = null;
			var len = list.length;
			switch(type) {
				case 'hotapps':
				    node = hotapplist;
					break;
				case 'userapps':
				    node = userapplist;
					break;
				default: 
					return;
			}
			if(!len || !node){
				return;
			}
			node.innerHTML = "";
			var lit = $.C('li'),app,li;
            for (var i = 0; i < len; i++) {
                app = list[i];
                li = lit.cloneNode(true);
                li.innerHTML = $.templet(LITEMPLATE,{name:app.name,aid:app.id});
                node.appendChild(li);
            }
        }
        
		$.gListener.register('hotapps', build);
        $.gListener.register('userapps', build);
		$.gListener.register('logout', function(){
            appmenu.style.display = "none";
        });
		
		var appTypeMenu = $.app.actui.downmenu({
			region :appselect,
			values:[{name:"热门应用",value:0},{name:"已安装的应用",value:1}],
			defaultValue:1,
			nodetype:"appselect",
			onchange:function(val){
				switch(val) {
					case '0':
					    hotapplist.style.display = "";
						userapplist.style.display = "none";
						break;
					case '1':
					    userapplist.style.display = "";
						hotapplist.style.display = "none";
						break;
					default: 
						break;
				}
			}
		});
        
        $.addEvent(addappbtn,"click",function(){
			appTypeMenu.change("0");
		});
		var aDelegate = $.core.evt.delegatedEvent(appmenu);
		aDelegate.add('addapp','click', function(spec){
			$.app.common.request({
	            url: 'app.s',
	            args: {
	                func: 'addapp',
					aid:spec.data.aid,
					uid:$.uid
	            }
	        });
		});
		
		$.app.common.request({
            url: 'app.s',
            args: {
                func: 'getapps',
				uid:$.uid
            }
        });
    };
});